import { saveOrganization } from '@/services/api/api-admin-organization';
import ProForm, {
  DrawerForm,
  ProFormDigit,
  ProFormRadio,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { Button, message } from 'antd';
import React from 'react';

interface AddOrganizationProps {
  parent: AdminOrganizationData.Organization;
  onRefurbish: () => void;
}
const AddOrganization: React.FC<AddOrganizationProps> = ({ parent, onRefurbish }) => {
  return (
    <DrawerForm
      title="新增"
      drawerProps={{
        destroyOnClose: true,
      }}
      trigger={<Button type="primary">新增</Button>}
      initialValues={{
        parentName: parent?.name,
        seq: 250,
        state: 1,
        isFolder: 0,
      }}
      onFinish={async (values) => {
        const response = await saveOrganization({
          ...values,
          parentId: parent?.id,
        });
        if (response.code === 0) {
          message.success('提交成功');
          onRefurbish();
        }
        return true;
      }}
    >
      <ProForm.Group>
        <ProFormText width="md" name="parentName" label="上级" disabled />
        <ProFormDigit width="md" label="排序" name="seq" min={1} max={250} />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText width="xl" name="avatar" label="头像" />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText
          width="md"
          name="name"
          label="名称"
          rules={[{ required: true, message: '请输入名称' }]}
        />
        <ProFormText
          width="md"
          name="nickname"
          label="昵称"
          rules={[{ required: true, message: '请输入昵称' }]}
        />
        <ProFormTextArea width="xl" name="describes" label="描述" />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormRadio.Group
          name="isFolder"
          label="文件夹"
          options={[
            {
              label: '是',
              value: 1,
            },
            {
              label: '否',
              value: 0,
            },
          ]}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormRadio.Group
          name="state"
          label="状态"
          options={[
            {
              label: '启用',
              value: 1,
            },
            {
              label: '禁用',
              value: 0,
            },
          ]}
        />
      </ProForm.Group>
    </DrawerForm>
  );
};
export default AddOrganization;
